# -*- Makefile -*- for Dungeon Crawl (Win32, MinGW)

# makefile.obj includes a list of object files needed to build Crawl.
include makefile.obj

ifeq ($(DEBUG_CRAWL),)
OPATH := rel
else
OPATH := dbg
endif

# need .exe so make will find the right file
APPNAME = $(OPATH)\crawl.exe
CXX = g++
DELETE = del
COPY = copy
OS_TYPE = WIN32TILES
EXTRA_DEFINES = -DUSE_TILE -DCLUA_BINDINGS

SDL_PREFIX ?= C:/mingw
SDL_CFLAGS := -D_GNU_SOURCE=1 -Dmain=SDL_main -I$(SDL_PREFIX)/include/SDL
SDL_LDFLAGS := -lmingw32 -lSDLmain -lSDL -mwindows

INSTALLDIR := $(OPATH)

# If you don't have flex or bison, set DOYACC to N or empty.
DOYACC := n

LEX  := flex
YACC := bison -y

ifeq ($(LUASRC),)
LUASRC := util\lua\src
endif

LUALIB = lua
LUALIBA = lib$(LUALIB).a

SQLSRC := util\sqlite
SQLLIB := sqlite3
SQLIBA   := lib$(SQLLIB).a
FSQLLIBA := $(SQLLIB)\$(SQLIBA)

PCRESRC := util\pcre
PCRELIB := pcre
PCRELIBA := lib$(PCRELIB).a

RLTILES = rltiles
EXTRA_INCLUDES += -I$(RLTILES)

TILEDEFS = dngn main player
TILEDEFPRES = $(TILEDEFS:%=$(RLTILES)/tiledef-%)
TILEDEFOBJS = $(TILEDEFPRES:%=%.o)
TILEDEFSRCS = $(TILEDEFPRES:%=%.cc)
TILEDEFHDRS = $(TILEDEFPRES:%=%.h)

OBJECTS += $(TILEDEFOBJS)

TILEFILES = \
    main.png \
    player.png \
    dngn.png

DESTTILEFILES = $(TILEFILES:%=dat/tiles/%)

LIB = -lwinmm -mwindows -lcomctl32 -L$(LUASRC) -l$(LUALIB) -L$(SQLSRC) -l$(SQLLIB) -L$(PCRESRC) -l$(PCRELIB) -lpng $(SDL_LDFLAGS) -lopengl32 -lglu32 -lSDL_image -lfreetype
INCLUDES := -Iutil -I. -I$(LUASRC) -I$(SQLSRC) -I$(PCRESRC) $(EXTRA_INCLUDES)

CFWARN := -Wall -Wwrite-strings -pedantic

CFOTHERS := -fsigned-char \
		-fstrict-aliasing \
		-pedantic \
		-O2 \
		-D$(OS_TYPE) $(EXTRA_FLAGS) \
		-DWINMM_PLAY_SOUNDS -DCLUA_BINDINGS \
		-DWINVER=0x0400 -D_WIN32_IE=0x0400 \
		$(EXTRA_DEFINES) \

CFLAGS   := $(INCLUDES) $(CFWARN) $(CFOTHERS) $(SDL_CFLAGS)
YCFLAGS  := $(INCLUDES) $(CFOTHERS)

OBJECTS := $(OBJECTS) libgui.o tilepick.o tile2.o tilereg.o tilesdl.o tilefont.o tiletex.o tilemcache.o tilebuf.o crash-w.o

LDFLAGS =

UTIL = util/

YTABC  := levcomp.tab.c
YTABH  := levcomp.tab.h

ifeq ($(LEX),)
DOYACC :=
endif

ifeq ($(YACC),)
DOYACC :=
endif

RESOURCE := $(UTIL)crawl.rc
RESOURCEOBJ := crawlres.o
WINDRES := windres.exe

# Do the levcomp stuff first because that's the most likely to fail.
OBJECTS      := levcomp.tab.o levcomp.lex.o \
		$(OBJECTS) $(RESOURCEOBJ)

OBJECTS := $(foreach file,$(OBJECTS),$(OPATH)/$(file))

GAME_DEPENDS := prepare $(LUASRC)\$(LUALIBA) $(FSQLLIBA) $(PCRESRC)\$(PCRELIBA) $(TILEHEADERS) $(DESTTILEFILES) $(OBJECTS)

##########################################################################

all:            $(APPNAME)

prepare:
		if not exist $(OPATH) mkdir $(OPATH)

DEPENDENCY_MKF := makefile.dep
-include $(DEPENDENCY_MKF)

##########################################################################
# The level compiler
#
ifeq ($(DOYACC),y)

# [ds] A plague on the broken copy command on Windoze.
prebuildyacc:	$(UTIL)levcomp.lex.cc $(UTIL)levcomp.tab.cc	$(UTIL)levcomp.tab.h
		$(subst /,\,for %%f in ($^) do $(COPY) %%f prebuilt)

$(UTIL)levcomp.tab.cc: $(UTIL)levcomp.ypp
		$(subst /,\, cd $(UTIL)) && $(YACC) -d -b levcomp levcomp.ypp
		$(subst /,\, cd $(UTIL)) && move $(YTABC) levcomp.tab.cc

$(UTIL)levcomp.lex.cc: $(UTIL)levcomp.lpp
		$(subst /,\, cd $(UTIL) && $(LEX) -olevcomp.lex.cc levcomp.lpp)

else

$(UTIL)levcomp.tab.cc: prebuilt/levcomp.tab.cc
		$(subst /,\,$(COPY) prebuilt/*.h $(UTIL))
		$(subst /,\,$(COPY) $< $@)

$(UTIL)levcomp.lex.cc: prebuilt/levcomp.lex.cc
		$(subst /,\,$(COPY) $< $@)

endif

##########################################################################
# RLTiles
#

ORIGTILEFILES = $(TILEFILES:%=$(RLTILES)/%)

makerltiles:
	pushd $(RLTILES) && $(MAKE) -f makefile.mgw all
	mkdir $(OPATH)\rltiles 2>nul || echo.>nul

$(TILEDEFSRCS): makerltiles

$(TILEDEFHDRS): makerltiles

$(ORIGTILEFILES): makerltiles

dat/tiles/%.png: rltiles/%.png
	$(subst /,\,$(COPY) $< $@)

clean-rltiles:
	pushd $(RLTILES) && $(MAKE) -f makefile.mgw distclean

##########################################################################


##########################################################################

install:        $(APPNAME)
ifneq ($(OPATH),$(INSTALLDIR))
		$(COPY) $(APPNAME) ${INSTALLDIR}
endif
		mkdir $(INSTALLDIR)\dat 2>nul || echo "" >nul
		mkdir $(INSTALLDIR)\dat\lua 2>null || echo "">nul
		mkdir $(INSTALLDIR)\dat\clua 2>null || echo "">nul
		mkdir $(INSTALLDIR)\dat\descript 2>null || echo "">nul
		mkdir $(INSTALLDIR)\dat\database 2>null || echo "">nul
		mkdir $(INSTALLDIR)\settings 2>null || echo "">nul
		mkdir $(INSTALLDIR)\docs 2>null || echo "">nul
		copy /y dat\*.des $(INSTALLDIR)\dat
		copy /y dat\lua\*.lua $(INSTALLDIR)\dat\lua
		copy /y dat\clua\*.lua $(INSTALLDIR)\dat\clua
		copy /y dat\descript\*.txt $(INSTALLDIR)\dat\descript
		copy /y dat\database\*.txt $(INSTALLDIR)\dat\database
		copy /y ..\settings\* $(INSTALLDIR)\settings
		copy /y ..\docs\* $(INSTALLDIR)\docs
		mkdir $(INSTALLDIR)\dat\tiles 2>nul || echo "" > nul
		copy /y dat\tiles\*.* $(INSTALLDIR)\dat\tiles
		copy /y ..\* $(INSTALLDIR)
		$(DELETE) $(INSTALLDIR)\*.sh

clean:
		$(DELETE) $(OPATH)\*.o
		$(subst /,\,$(DELETE) $(UTIL)*.o)
		$(subst /,\,$(DELETE) $(UTIL)*.exe)
		$(subst /,\,$(DELETE) $(UTIL)*.lex.cc)
		$(subst /,\,$(DELETE) $(UTIL)*.tab.cc)
		$(subst /,\,$(DELETE) $(UTIL)*.tab.h)
		$(subst /,\,$(DELETE) $(UTIL)*.tab.c)
		$(subst /,\,$(DELETE) *.ixx)

clean-lua:
	    cd $(LUASRC) && $(MAKE) clean_win

clean-sql:
	    cd $(SQLSRC) && $(MAKE) "RM_F=del /f" clean

clean-pcre:
	    cd $(PCRESRC) && $(MAKE)

distclean: clean clean-lua clean-sql clean-pcre clean-rltiles
		$(DELETE) dbg\*.o
		$(DELETE) rel\*.o
		$(DELETE) *.o
		$(DELETE) bones.*
		$(DELETE) $(OPATH)\bones.*
		$(DELETE) morgue.txt
		$(DELETE) $(OPATH)\morgue.txt
		$(DELETE) scores
		$(DELETE) $(OPATH)\scores
		$(DELETE) crawl.exe
		$(DELETE) $(subst /,\,$(APPNAME))
		$(DELETE) *.sav
		$(DELETE) $(OPATH)\*.sav
		$(DELETE) core
		$(DELETE) $(OPATH)\core
		$(DELETE) *.0*
		$(DELETE) $(OPATH)\*.0*
		$(DELETE) *.lab
		$(DELETE) $(OPATH)\*.lab

$(APPNAME):	$(GAME_DEPENDS)
		${CXX} ${LDFLAGS} $(CFLAGS) $(OBJECTS) -o $(APPNAME) $(LIB)
		strip $(APPNAME)

debug:		$(GAME_DEPENDS)
		${CXX} ${LDFLAGS} $(CFLAGS) $(OBJECTS) -o $(APPNAME) $(LIB)

profile:	$(GAME_DEPENDS)
		${CXX} -g -p ${LDFLAGS} $(CFLAGS) $(OBJECTS) -o $(APPNAME) $(LIB)

$(OPATH)/%.o: %.cc $(TILEDEFHDRS)
		${CXX} ${CFLAGS} -o $@ -c $<

# STL is bad in debug.  Always compile this file with optimizations.
$(OPATH)/tilebuf.o: tilebuf.cc $(TILEDEFHDRS)
		${CXX} ${CFLAGS} -O2 -o $@ -c $<


$(OPATH)/%.o: $(UTIL)%.cc $(TILEDEFHDRS)
		$(CXX) $(YCFLAGS) -o $@ -c $<

#############################################################################
# Build Lua

$(LUASRC)\$(LUALIBA):
	@echo Building Lua...
	@cd $(LUASRC) && $(MAKE) crawl_mingw

#############################################################################
# Build SQLite

$(FSQLLIBA):
	@echo Building SQLite
	cd $(SQLSRC) && $(MAKE)

#############################################################################
# Build PCRE

$(PCRESRC)\$(PCRELIBA):
	@echo Building PCRE...
	@cd $(PCRESRC) && $(MAKE)

##########################################################################
# Resources
#

$(OPATH)/$(RESOURCEOBJ): $(RESOURCE)
	$(WINDRES) $< $@
